package com.woniu.stafforder.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class NettyServer {
    private final int port;

    public NettyServer(int port) {
        this.port = port;
    }

    public void start() throws Exception {
        // 使用自签名证书
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        File cert = new File("C:\\Users\\0\\Desktop\\key\\cert.pem");
        File key = new File("C:\\Users\\0\\Desktop\\key\\key.pem");
        String pfxFilePath = "C:\\Users\\0\\Desktop\\key\\cert.pfx";
        String password = "cuibo";
        SslContext sslContext = SslContextBuilder.forServer(cert,key).build();
        System.out.println("生成证书"+sslContext.toString());
//        SslContext sslCtx = SslContextBuilder.forServer(certFile, keyFile)
//                .keyManager(certFile,keyFile)
//                .protocols("TLSv1.2") // 设置支持的协议版本
//                .clientAuth(ClientAuth.NONE) // 设置客户端认证方式
//                .build();
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .localAddress(port)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
//                            SSLEngine sslEngine = sslCtx.newEngine(ch.alloc());
                            ch.pipeline().addLast(sslContext.newHandler(ch.alloc()));
//                            ch.pipeline().addLast(new SslHandler(sslEngine));
                            ch.pipeline().addLast(new HttpServerCodec());
                            ch.pipeline().addLast(new ChunkedWriteHandler());
                            ch.pipeline().addLast(new HttpObjectAggregator(1048576));
                            ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws"));
                            ch.pipeline().addLast(new MyServerHandler());
                        }
                    });
            ChannelFuture future = bootstrap.bind(port).sync();
            future.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
